<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;

/**
 * Class DormitoryRating.
 *
 * @package namespace App\Models;
 */
class DormitoryRating extends BaseModel implements Transformable
{
    use TransformableTrait;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['dorm_id', 'user_id', 'period_id', 'subitem_id', 'subitem_score', 'score', 'remark', 'rating_date'];

    public function dorm()
    {
        return $this->belongsTo(Dorm::class);
    }
    public function user()
    {
        return $this->belongsTo(User::class);
    }
    public function period()
    {
        return $this->belongsTo(Period::class);
    }
    public function dormRatingItem()
    {
        return $this->belongsTo(RatingSubitem::class, 'subitem_id');
    }
    public function dormRatingMain()
    {
        return $this->belongsTo(DormRatingMain::class);
    }

    protected static function boot()
    {
        parent::boot();
        static::creating(function ($model) {
            $model->subitem_score = RatingSubitem::find($model->subitem_id)->subitem_score;
        });
    }
}
